﻿using FasterWPF;
using FasterWPFDemo.UserControls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace FasterWPFDemo
{
    public partial class TabItem04 : UserControl, IUserControlGuid
    {
        private string _userCtlGuid = null;
        private bool temporarilyDisableSelectionChanged = false;

        public string UserControlGuid
        {
            get
            {
                return _userCtlGuid;
            }

            set
            {
                _userCtlGuid = value;
            }
        }

        public TabItem04()
        {
            //InitializeComponent(); XAML removed. Initialize via code-behind.

            this.Initialize(ContainerType.VerticalPanel);

            this.Width = 934D;
            this.Height = 850D;

            this.BeginSettings<UserControl>()
                .SetSelectorClass<UserControl>(0, 0, "tabLabel")
                .SetItemBorderColorAndThickness<UserControl>(Brushes.Gray, new Thickness(3)).EndSettings();
        }

        public void LoadUserControl()
        {
            StackPanel foundation = new StackPanel();
            foundation.Initialize(double.NaN, double.NaN, ContainerType.HorizontalPanel, Orientation.Horizontal);
            foundation.Margin = new Thickness(0);

            foundation.BeginSettings<StackPanel>().SetItemBorderSettings<StackPanel>(double.NaN, double.NaN, Brushes.Black, new Thickness(0)).EndSettings<StackPanel>();

            TreeView trv = null;
            trv = new TreeView();
            trv.Name = "myTreeView";
            
            string trvguid5 = Guid.NewGuid().ToString();
            string anotherGuid = Guid.NewGuid().ToString();

            ////Add TreeView to Grid at row 1 column 0
            //myGrid.BeginComposite<Grid>(gridguid4)
            //    .AddExisting<TreeView, Grid>(0, 0, trv)
            //    .EndComposite<Grid, GridArgs>(new GridArgs(1, 0));

            //myGrid.UnsubscribeMouseDoubleClickByKey(gridguid4);
            //
            trv.Initialize(475D, 815, ContainerType.Grid, 7, 3);
            trv.SetBackgroundColor(Brushes.WhiteSmoke);
            trv.Margin = new Thickness(30D, 0, 15D, 0);
            
            trv.SetCompositeGridDimensionsWithRowHt<TreeView>(3, 2, .37D, GridUnitType.Star, .5, GridUnitType.Star, colWidth2: .5, unitType2: GridUnitType.Star);

            TreeViewExt.SetTreeViewItemBackgroundColor(trv, Brushes.WhiteSmoke);
            TreeViewExt.SetTreeViewItemForegroundColor(trv, Brushes.Black);

            trv.SetArrowColors("WhiteSmoke", "Silver", "Black", "Blue", "Silver", "Black", 20, 23, "LightBlue", "Black", "Khaki", "Black");

            trv.BeginSettings<TreeView>()
                //.SetBackgroundColorOnItemsControl(BrushExt.CreateGradientBrush("#DDDDDD", 25, "#33AAFF", 25, 1, 50, 1, 50))            //chain.SetBackgroundColor(229, 228, 226, 255);

                .CreateFontStyleKey("mainFont", "Segoe UI", 27D, FontWeights.Bold, FontStyles.Normal)
                .CreateColorStyleKey("mainColor", Brushes.DarkBlue) //143, 121, 100, 255)
                .SetItemBorderSettingsFull(375D, 57D, Brushes.Silver, new Thickness(2), new CornerRadius(4),new Thickness(3))
                .SetFont<TreeView>(0, 0, "Verdana", 14D, FontWeights.Bold, FontStyles.Normal, null)
                .SetTextColor<TreeView>(0, 0, Brushes.DarkBlue)

             //   .SetItemBorderColorAndThickness(Brushes.MidnightBlue, new Thickness(0, 0, 1, 0))
                //.SetGridProperties(3, 3, 29, GridUnitType.Pixel, 95, GridUnitType.Pixel, 310, GridUnitType.Pixel)      
                //.SetRowPadding(new Thickness(10, 10, 10, 10))
                //.SetScrollViewerPadding(new Thickness(10, 10, 10, 10)) 
                //.SetImageMargin(0, 0, new Thickness(0, 0, 0, 0))
                // .SetImageStretch(0, 0, Stretch.Fill)
                //  .SetImageHorizontalAlignment(0, 0, HorizontalAlignment.Left)
                 .SetTextPadding(0, 0, new Thickness(3, 0, 1, 0))
                 .SetTextPadding(1, 0, new Thickness(3, 0, 1, 0))
                 .SetTextPadding(2, 0, new Thickness(3, 0, 1, 2))
                 .Set<TextBlock, TreeView>(2, 0, "Margin", new Thickness(0, 0, 0, 2))
                 .SubscribeEventOnParent<TreeView, RoutedPropertyChangedEventArgs<object>>("SelectedItemChanged", (trv_SelectedItemChanged))
                // .SubscribeMouseDoubleClick(new MouseButtonEventHandler(my_MouseDoubleClick), item => { return item.GetText<ListBox>(1, 0).Contains("Lake"); })
                //change this to use Rx and follow same IDisposable pattern?  Wire into Dispose method call?!?
                //    .SubscribeSelectionChanged(new SelectionChangedEventHandler(myTreeView_SelectionChanged));

                    ////VERTICAL SCROLLBAR
                .SetVerticalScrollBarColor(Brushes.WhiteSmoke)
                .SetVerticalScrollUpButtonColors("White", "Orange", "White", "#0088FF", "#DDDDDD", "0", "Gray", new Thickness(1), "Black", "Orange", "M 0 4 L 7 4 L 3.5 0 Z")
                .SetVerticalPageUpButtonColors("#DDDDDD", "Gray", "Gray", "#DDDDDD") //, "2", "White", new Thickness(3, 3, 3, 3))
                .SetVerticalPageDownButtonColors("#DDDDDD", "#DDDDDD", "Gray", "Gray") //,"5", "Gray", new Thickness(3, 3, 3, 3))
                .SetVerticalScrollDownButtonColors("White", "Orange", "#DDDDDD", "#0088FF", "White", "0", "Gray", new Thickness(1), "Black", "Orange", "M 0 0 L 3.5 4 L 7 0 Z")
                .SetVerticalScrollThumbColors("#0088FF", "#0088FF", "Gray", "#DDDDDD", "1", "1")

                //HORIZONTAL SCROLLBAR
                .SetHorizontalScrollBarColor(Brushes.WhiteSmoke)
                .SetHorizontalScrollRightButtonColors("#BBB", "Blue", "Red", "Red", "Red", "0", "#FFEE", new Thickness(1), "Black", "Orange", "M 0 0 L 4 4 L 0 8 Z")
                .SetHorizontalPageRightButtonColors("#DDDDDD", "#DDDDDD", "Gray", "Gray") //, "#FFEE", "0", new Thickness(0, 0, 0, 0))
                .SetHorizontalPageLeftButtonColors("#DDDDDD", "Gray", "Gray", "#DDDDDD") // , "#FFEE", "0", new Thickness(0, 0, 0, 0))
                .SetHorizontalScrollLeftButtonColors("#BBB", "Blue", "Red", "Red", "Red", "0", "#FFEE", new Thickness(1), "Black", "Orange", "M 4 0 L 4 8 L 0 4 Z")
                .SetHorizontalScrollThumbColors("#0088FF", "#0088FF", "Gray", "#DDDDDD", "1", "1")

                .SetScrollViewerCornerRectangleColor(Brushes.White)
             
                .EndSettings<TreeView>();

            int itrv = 0;
            List<Travel> listOfTravelItems;
            listOfTravelItems = new List<Travel>();            
            Enumerable.Range(0, 100).ToList().ForEach(x => { itrv = populateTravelEntities(listOfTravelItems, itrv, x); });
            Border firstChild = null;

            listOfTravelItems.ForEach(trav =>
            {
                if (trav.Counter == 0)
                {
                    //PARENT: leave treeviewargs guid null to signify root treeviewitem
                    trv.BeginComposite<TreeView>(trav.ID)
                            .AddText<TreeView>(0, 0, trav.Counter.ToString())
                            .AddText<TreeView>(1, 0, "Flight: " + trav.Flight)
                            .AddText<TreeView>(1, 1, "Hotel: " + trav.Hotel)
                            .AddText<TreeView>(2, 0, "Attraction: " + trav.Attraction)
                            .SetMouseOverColorOnContainer<Grid, TreeView>(Brushes.Aquamarine)
                            .EndComposite<TreeView, TreeViewArgs>(new TreeViewArgs(null));

                    //CHILD: add guid to treeviewargs to tell it where to tack onto
                    firstChild = trv.BeginComposite<TreeView>(trav.ParkingGarages[0].ID)
                            .AddText<TreeView>(0, 0, "Color: " + trav.ParkingGarages[0].Color)
                            .AddText<TreeView>(1, 0, "Description: " + trav.ParkingGarages[0].Description)
                            .AddText<TreeView>(2, 0, " ")
                            .SetMouseOverColorOnContainer<Grid, TreeView>(Brushes.Aquamarine)
                            .EndComposite<TreeView, TreeViewArgs>(new TreeViewArgs(trav.ID));
                }
                else
                {
                    //PARENT: leave treeviewargs guid null to signify root treeviewitem
                    trv.BeginComposite<TreeView>(trav.ID)
                            .AddText<TreeView>(0, 0, trav.Counter.ToString() )
                            .AddText<TreeView>(1, 0, "Row 1 Col 0: " + trav.Flight)
                            .AddText<TreeView>(1, 1, "Row 1 Col 1: " + trav.Hotel)
                            .AddText<TreeView>(2, 0, "Row 2 Col 0: " + " " + trav.Attraction)
                            .SetMouseOverColorOnContainer<Grid, TreeView>(Brushes.Aquamarine)
                            .EndComposite<TreeView, TreeViewArgs>(new TreeViewArgs(null));

                    //CHILD: add guid to treeviewargs to tell it where to tack onto
                    trv.BeginComposite<TreeView>(trav.ParkingGarages[0].ID)
                            .AddText<TreeView>(0, 0, "2nd Item: " + trav.ParkingGarages[0].Color)
                            .AddText<TreeView>(1, 0, "Row 1 Col 0: " + trav.ParkingGarages[0].Description)
                             .AddText<TreeView>(2, 0, " ")
                            .SetMouseOverColorOnContainer<Grid, TreeView>(Brushes.Aquamarine)
                            .EndComposite<TreeView, TreeViewArgs>(new TreeViewArgs(trav.ID));
                }
            });

            //Append a few extra on the first child
            trv.BeginComposite<TreeView>(trvguid5)
                    .AddText<TreeView>(1, 0, "3rd Item: yacht")
                    .SetMouseOverColorOnContainer<Grid, TreeView>(Brushes.Aquamarine)                    
                    .EndComposite<TreeView, TreeViewArgs>(new TreeViewArgs(firstChild.GetKey()));

            trv.BeginComposite<TreeView>(anotherGuid)
                    .AddText<TreeView>(1, 0, "3rd Item: sailboat")
                    .SetMouseOverColorOnContainer<Grid, TreeView>(Brushes.Aquamarine)
                    .EndComposite<TreeView, TreeViewArgs>(new TreeViewArgs(firstChild.GetKey()));
            

            //Add the TreeView to the StackPanel
            foundation.BeginComposite<StackPanel>()
                 .AddExisting<TreeView, StackPanel>(0, 1, trv)
                 .AddFlatButton<StackPanel>(0, 2, 120D, 120D,
                "Sort Parent\n Nodes Desc", Brushes.GhostWhite, new Thickness(2), Brushes.MediumAquamarine, new CornerRadius(3), new Thickness(2), new Thickness(3), Brushes.LightBlue,
                (o, ev) =>
                {
                    temporarilyDisableSelectionChanged = true;
                    TreeViewSort treeVwSrt = new TreeViewSort(CommonExt.SortDataType.Integer);
                    trv.SortByText<TreeView>(treeVwSrt, 0, 0, CommonExt.SortOrder.Desc, CommonExt.SortControlType.TextBlock);
                    temporarilyDisableSelectionChanged = false;

                }).AddFlatButton<StackPanel>(0, 3, 120D, 120D,
                "Sort Parent\n Nodes Asc\n As Text", Brushes.GhostWhite, new Thickness(2), Brushes.MediumAquamarine, new CornerRadius(3), new Thickness(2), new Thickness(3), Brushes.LightBlue,
                (o, ev) =>
                {
                    temporarilyDisableSelectionChanged = true;
                    TreeViewSort treeVwSrt = new TreeViewSort(CommonExt.SortDataType.String);
                    trv.SortByText<TreeView>(treeVwSrt, 0, 0, CommonExt.SortOrder.Asc, CommonExt.SortControlType.TextBlock);
                    temporarilyDisableSelectionChanged = false;

                }).AddFlatButton<StackPanel>(0, 4, 120D, 120D,
                "Sort Parent\n Nodes Asc\n As Int", Brushes.GhostWhite, new Thickness(2), Brushes.MediumAquamarine, new CornerRadius(3), new Thickness(2), new Thickness(3), Brushes.LightBlue,
                (o, ev) =>
                {
                    temporarilyDisableSelectionChanged = true;
                    TreeViewSort treeVwSrt = new TreeViewSort(CommonExt.SortDataType.Integer);
                    trv.SortByText<TreeView>(treeVwSrt, 0, 0, CommonExt.SortOrder.Asc, CommonExt.SortControlType.TextBlock);
                    temporarilyDisableSelectionChanged = false;

                })
                .EndComposite<StackPanel, StackPanelArgs>(null);

            //Set the Label for the Tab Item Header and Add the StackPanel to the UserControl
            this.BeginComposite<UserControl>(_userCtlGuid)
                .AddLabel<UserControl>(0, 0, "Tab Item 4 -  Tree View Example", Brushes.Turquoise)
                .AddExisting<StackPanel, UserControl>(0, 1, foundation)   
                .EndComposite<UserControl, UserControlArgs>(null);
        }

        private int populateTravelEntities(List<Travel> tListTreeView, int i, int x)
        {
            int j = i;

            List<ParkingGarage> pgList = new List<ParkingGarage>();

            pgList.Add(new ParkingGarage() 
            { 
                ID = Guid.NewGuid().ToString(),
                Color = "Gray", 
                Description="Gray as a Goose"
            });

            tListTreeView.Add(new Travel()
            { 
                 ID = Guid.NewGuid().ToString(), 
                 Attraction = "Lake Michigan",
                 Flight = "ORD",
                 Hotel = "XYZ Hotel",
                 ParkingGarages = pgList, 
                 Counter = x
            });

            if (i < 2)
            {
                i++;
            }
            else
            {
                i = 0; //reset
            }
            return i;
        }
    }
}
